package com.example.hotelsystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.hotelsystem.entity.City;
import com.example.hotelsystem.entity.HotelName;
import com.example.hotelsystem.entity.vo.CitySearchVO;
import com.example.hotelsystem.entity.vo.HotelNameSearchVO;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.HotelNameService;
import com.example.hotelsystem.vo.hotelname.HotelAddVO;
import com.fasterxml.jackson.databind.util.BeanUtil;
import io.swagger.annotations.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-07
 */
@RestController
@RequestMapping("/hotelsystem/hotelName")
@Api(tags = "酒店名称控制器")
public class HotelNameController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    HotelNameService hotelNameService;

    @PostMapping("/list")
    public Result HotelSearchList(HotelNameSearchVO hotelNameSearchVO){
        try{
            IPage<HotelName> hotels=hotelNameService.HotelSearchList(hotelNameSearchVO);
            if(hotels!=null){
                return Result.success(hotels);
            }
            return Result.error("找不到城市");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/removeById")
    public Result removeById(Long id){
        try{
            HotelName hotelName = hotelNameService.getById(id);
            hotelName.setIsDelete(1);
            hotelNameService.updateById(hotelName);
            return Result.success("操作成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/view")
    public Result hotelNameView(Long id){
        try{
            HotelName hotelName = hotelNameService.getById(id);
            return Result.success(hotelName);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @PostMapping("/update")
    public Result updateHotelName(HotelName hotelName){
        try{
            hotelNameService.updateById(hotelName);
            return Result.success("操作成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    public Result addHotelName(HotelAddVO vo){
        try{
            HotelName hotelName = new HotelName();
            BeanUtils.copyProperties(vo,hotelName);
            Date date = new Date();
            hotelName.setCreateTime(date);
            boolean isAdd = hotelNameService.save(hotelName);
            if(isAdd){
                return Result.success("酒店名称新增成功");
            }
            return Result.error("新增失败");
        }catch (Exception e){
            logger.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

}

